<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:40
 */

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Providers\MyJwt;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class HroController extends Controller
{

    private $uid;

    public function __construct(Request $request)
    {
        parent::__construct();
        $authorization = $request->header('Authorization');
        $jwt = new MyJwt();
        $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
        if (!$jwtCode || !array_key_exists('uid', $jwtCode)) {
            $this->sendJson([], 4031, '请登录');
        }
        if (!array_key_exists('role', $jwtCode) || $jwtCode['role'] != 2) {
            $this->sendJson([], 4032, '非法请求');
        }
        if (!array_key_exists('timeout', $jwtCode) || $jwtCode['timeout'] < time()) {
            $this->sendJson([], 4031, '请登录');
        }

        $cacheKey = sprintf('ht:login:uid:%d', $jwtCode['uid']);
        $cacheData = Redis::get($cacheKey);
        if (!$cacheData || $cacheData < time()) {
            Redis::setex($cacheKey, 0, null);
            $this->sendJson([], 4031, '请登录');
        }
        $this->jwtCode = $jwtCode;
        $this->uid = $jwtCode['uid'];
    }

    public function teacherList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $search = $request->get('search');
            $status = $request->get('status');
            $option = [];
            if ($search) {
                $option['query'][] = [
                    'key' => 'realname',
                    'operator' => 'like',
                    'value' => "%{$search}%"
                ];
            }
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status - 1
                ];
            }
            $data = UserService::getTeacherList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function teacherDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = UserService::getTeacherDetail($id);
            $this->sendJson($data);
        });
    }

    public function teacherCheck(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            UserService::checkTeacher($data);
            $this->sendJson([], 200, '审核完成');
        });
    }

    public function teacherDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            UserService::deleteTeacher($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function teacherScoreList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = UserService::getTeacherScoreList($id);
            $this->sendJson($data);
        });
    }

    public function saveScore(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            UserService::saveTeacherScore($data);
            $this->sendJson([], 200, '打分成功');
        });
    }
}